/*
This file is part of the replication package of "Asset-Price Redistribution" by
Fagereng, Gomez, Gouin-Bonenfant, Holm, Moll, and Natvik. 

This file produces a binscatter of interest rated ltvs; and runs two regressions
of interest rates on ltvs   

*/

********************************************************************************
u year lnr hhid edlevel wealth_a net_private_debt_a debt*_a bonds_a carboat_a deposits*_a stocks*_a mfund_a housing*_a b_year cpi earned_income_a transfers_a age *PB* beq_* parent* death cap_inc* cap_exp* nt_* earned_income_a transfers_a LTV*a using $data/welfare_9319 if death != 1 & b_year != ., clear

tsset lnr year
drop if wealth_a == 0

* compute interest rate using interest expenses and average debt over the year
gen interest_rate = cap_exp_interest_a / (debt_a + l.debt_a)*2 * 100
replace interest_rate = . if interest_rate == 0
* ltv = debt/housing wealth; use lagged ltv which is the same as ltv at the beginning of the year
gen l_LTV_a = l.LTV_a

* trim interest rates at top/bottom 5% per year
winsor2 interest_rate if year >= 1994 & year <= 2019, by(year) cuts(5 95) trim replace

* focus on relevant ltv range
replace l_LTV_a = . if l_LTV_a < 0.2 | l_LTV_a > 0.99
replace interest_rate = . if l_LTV_a < 0.2 | l_LTV_a > 0.99

* for binscatter: take out average per year, add in average across all years
bys year: egen _mean_ir = mean(interest_rate)
egen mean_ir_all = mean(interest_rate)
gen interest_rate_demeaned = interest_rate - _mean_ir + mean_ir_all
binscatter interest_rate_demeaned l_LTV_a, n(100) line(none) xtitle("Loan-to-value in %") ytitle("Interest Rate on Mortgage in %") savedata($output/interest_rate_and_ltv) replace graphregion(color(white))

* remove the do-file that binscatter makes
erase $output/interest_rate_and_ltv.do


* for regression: use interest rate in number (0.05 instead of 5%)
replace interest_rate = interest_rate/100

* run regressions
eststo clear
eststo: qui areg interest_rate l_LTV_a, absorb(year) robust
eststo: qui areg interest_rate l_LTV_a i.age i.edlevel, absorb(year) robust
esttab, keep(*LTV_a*) se(10) b(10) r2 plain
esttab using $output/interest_rate_and_ltv_regression.csv, keep(*LTV_a*) se(10) b(10) r2 replace plain

clear
